package be.kuleuven.cs.mop.gui.dialogs;


import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;

import be.kuleuven.cs.mop.gui.components.ActionButton;
import be.kuleuven.cs.mop.gui.components.InfoLabel;
import be.kuleuven.cs.mop.gui.components.SelectionList;
import be.kuleuven.cs.mop.gui.util.SwingUtils;



/**
 * {@link Dialog} implementation used to ask yes/no style questions
 * 	with an option for aditional data/information
 */
public class QuestionDialog extends Dialog implements ActionListener {
	private static final long serialVersionUID = 1L;
	
	private boolean answer;
	
	
	public <T> QuestionDialog(final Frame parent, final String question,
			final Collection<T> data) {
		super(parent, "Question");
		
		final GridBagConstraints gbc = SwingUtils.getGBC();
		
		gbc.gridx	= 0;
		gbc.gridy	= 0;
		gbc.gridwidth	= 2;
		add(new InfoLabel(question), gbc);
		
		gbc.gridy	= 1;
		add(new SelectionList<T>(data, false), gbc);
		
		gbc.gridy	= 2;
		gbc.gridwidth	= 1;
		add(new AnswerButton("Yes", true), gbc);
		
		gbc.gridx	= 1;
		add(new AnswerButton("No", false), gbc);
		
		display();
	}
	
	
	
	@Override
	public void actionPerformed(final ActionEvent event) {
		answer = ((AnswerButton)event.getSource()).answer;
		dispose();
	}
	
	public boolean getAnswer() { return answer; }
	
	
	
	
	
	
	
	
	
	
	@SuppressWarnings("serial")
	private class AnswerButton extends ActionButton {
		
		private final boolean answer;
		
		
		protected AnswerButton(final String label, final boolean answer) {
			super(label, new Dimension(150, 32), QuestionDialog.this);
			this.answer = answer;
		}
		
	}
	
}